package com.amikomgamedev.labirinhantu.state;

import org.andengine.entity.modifier.MoveXModifier;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.Server_Data;
import com.amikomgamedev.labirinhantu.Utils;

public class State_Menu_Select_Stage extends State_Menu_Main
{

	private final float	DURATION	= 0.3f;

	private	float[] minX	= new float[3];
	private	float[] maxX	= new float[3];
	
	private float	minCameraX;
	private float	centerCameraX;
	private	float	maxCameraX;
	
//	private boolean isStageSelectMove	= false;

	protected void create_Menu_Select_Stage()
	{
	    minCameraX		= RES_WIDTH;
	    centerCameraX	= RES_WIDTH + RES_WIDTH / 2;
	    maxCameraX		= RES_WIDTH * 2;
	    

		isCameraMove = false;
	}

	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
			if (isCameraMove)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);

				if (m_Camera_Menu.isStopCamera())
					// grafik.setPosMenuSelectStage();
					switchState(STATE_MENU_MAIN);
			}
		}
		super.onUpdate(pSecondsElapsed);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
			{
				Utils.TRACE("Pencet back");
				// grafik.detachBtnMenuSelectStage();
				m_Camera_Menu.initCamera(grafik.spr_Menu_Main_Bg);
				isCameraMove = true;
				grafik.setPosMenuSelectStage();
			}
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onScroll(ScrollDetector pScollDetector, int pPointerID, float pDistanceX, float pDistanceY)
	{
		if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
			for (int i = 0; i < MENU_SELECT_STAGE_BG.length; i++)
			{
				grafik.spr_Menu_Select_Stage[i].setPosition(grafik.spr_Menu_Select_Stage[i].getX() + pDistanceX,
						grafik.spr_Menu_Select_Stage[i].getY());
			}

		}
		super.onScroll(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}
	
	@Override
	public void onScrollFinished(
	    ScrollDetector pScollDetector,
	    int pPointerID,
	    float pDistanceX,
	    float pDistanceY)
	{	    
	    if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
		    for (int i = 0; i < minX.length; i++)
			minX[i]	= grafik.spr_Menu_Select_Stage[i].getX();
		    
		    for (int i = 0; i < maxX.length; i++)
			maxX[i]	= grafik.spr_Menu_Select_Stage[i].getX() + grafik.spr_Menu_Select_Stage[i].getWidth();
		    
		    // stage 1 digeser ke kanan
		    if (minX[0] > minCameraX)
		    {
			float[] pToX	= {RES_WIDTH, RES_WIDTH * 2, RES_WIDTH * 3};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}
			
		    }

		    // stage 3 digeser ke kiri
		    if (maxX[2] < maxCameraX)
		    {
			float[] pToX	= {-RES_WIDTH, 0, RES_WIDTH};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}			
		    }
		    
		    // antara stage 1 dan stage 2 digeser ke stage 1
		    if (maxX[0] > centerCameraX && maxX[0] < maxCameraX)
		    {
			float[] pToX	= {RES_WIDTH, RES_WIDTH * 2, RES_WIDTH * 3};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Stage_Lingkaran.get(0).setCurrentTileIndex(0);
			grafik.lst_Menu_Stage_Lingkaran.get(1).setCurrentTileIndex(1);
			grafik.lst_Menu_Stage_Lingkaran.get(2).setCurrentTileIndex(1);
		    }
		    
		    // antara stage 1 dan stage 2 digeser ke stage 2
		    if (minX[1] < centerCameraX && minX[1] > minCameraX)
		    {
			float[] pToX	= {0, RES_WIDTH, RES_WIDTH * 2};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Stage_Lingkaran.get(0).setCurrentTileIndex(1);
			grafik.lst_Menu_Stage_Lingkaran.get(1).setCurrentTileIndex(0);
			grafik.lst_Menu_Stage_Lingkaran.get(2).setCurrentTileIndex(1);
		    }
		    
		    // antara stage 2 dan stage 3 digeser ke stage 2
		    if (maxX[1] > centerCameraX && maxX[1] < maxCameraX)
		    {
			float[] pToX	= {0, RES_WIDTH, RES_WIDTH * 2};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Stage_Lingkaran.get(0).setCurrentTileIndex(1);
			grafik.lst_Menu_Stage_Lingkaran.get(1).setCurrentTileIndex(0);
			grafik.lst_Menu_Stage_Lingkaran.get(2).setCurrentTileIndex(1);
		    }
		    
		    // antara stage 2 dan stage 3 digeser ke stage 3
		    if (minX[2] < centerCameraX && minX[2] > minCameraX)
		    {
			float[] pToX	= {- RES_WIDTH, 0, RES_WIDTH};
			
			for (int i = 0; i < pToX.length; i++)
			{
//			    isStageSelectMove	= true;
				grafik.spr_Menu_Select_Stage[i].registerEntityModifier(
					new MoveXModifier(DURATION, minX[i], pToX[i]));			    
			}

			grafik.lst_Menu_Stage_Lingkaran.get(0).setCurrentTileIndex(1);
			grafik.lst_Menu_Stage_Lingkaran.get(1).setCurrentTileIndex(1);
			grafik.lst_Menu_Stage_Lingkaran.get(2).setCurrentTileIndex(0);
		    }
		    
//		    if (isStageSelectMove)
//		    {
//			m_Scene.registerUpdateHandler(new TimerHandler(DURATION, new ITimerCallback()
//			{
//			    
//			    public void onTimePassed(TimerHandler pTimerHandler)
//			    {
//				isStageSelectMove	= false;
//			    }
//			}));
//		    }
		    
		    
		 
		}
	    super.onScrollFinished(pScollDetector, pPointerID, pDistanceX, pDistanceY);
	}

	@Override
	public void onClick(ClickDetector pClickDetector, int pPointerID, float pSceneX, float pSceneY)
	{
		if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
			for (int i = 0; i < MENU_SELECT_STAGE_BG.length; i++)
			{
				if (Utils.isOnArea(pSceneX, pSceneY, grafik.spr_Menu_Select_Stage[i]))
				{
					Server_Data.setStage(i);
					Server_Data.setMap(0);
					grafik.setPosMenuSelectStage();
					// pindah ke menu level
					switchState(STATE_MENU_LEVEL);
				}
			}

		}
		super.onClick(pClickDetector, pPointerID, pSceneX, pSceneY);
	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		if (State_Menu_Current == STATE_MENU_SELECT_STAGE)
		{
			m_ClickDetector.onTouchEvent(pSceneTouchEvent);
			m_Scroll.onTouchEvent(pSceneTouchEvent);
		}
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_MENU_SELECT_STAGE)
		sound.stopMenuBgm();
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_MENU_SELECT_STAGE)
		sound.playMenuBgm();

	    super.onResumeGame();
	}

}
